<?php

namespace app\admin\controller\system;

use app\Base;
use app\service\admin\WebsiteService;
use app\validate\admin\WebsiteValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

/**
 * 控制器
 * Class Website
 * @package projects\controller\admin
 * @author Du
 * @date 2021/2/27
 * @time 17:15
 */
class Website extends Base
{

    /**
     * 网站设置页面
     *
     * User : Du
     * Date : 2022/3/31
     * Time : 9:57
     * @param WebsiteService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function show(WebsiteService $service): View
    {
        $res = $service::getWebsite();
        return view('system/website', ['data' => $res]);
    }

    /**
     * 修改数据
     * @param WebsiteService $service
     * @param WebsiteValidate $validate
     * @return Json
     * @throws DbException
     * @author Du
     * @date 2021/3/29 18:20
     */
    public function update(WebsiteService $service, WebsiteValidate $validate): Json
    {
        $res = $validate->check($this->params);
        if (!$res) {
            return $this->returnData('fail', $validate->getError());
        }
        $res = $service::updateWebsite(input());
        if (!$res) {
            return $this->returnData('updateFail');
        }
        return $this->returnData();
    }
}